import request from './request'

/**
 * 用户登录
 */
export const login = (data) => {
  return request({
    url: '/user/login',
    method: 'post',
    data
  })
}

/**
 * 用户注册
 */
export const register = (data) => {
  return request({
    url: '/user/register',
    method: 'post',
    data
  })
}

/**
 * 根据用户ID获取用户信息
 */
export const getUserById = (userId) => {
  return request({
    url: `/user/${userId}`,
    method: 'get'
  })
}

/**
 * 根据用户名获取用户信息
 */
export const getUserByUsername = (username) => {
  return request({
    url: `/user/username/${username}`,
    method: 'get'
  })
}

/**
 * 获取用户完整信息（包含角色、权限、菜单）
 */
export const getUserInfo = (userId) => {
  return request({
    url: `/user/info/${userId}`,
    method: 'get'
  })
}

/**
 * 分页查询用户列表
 */
export const getUserList = (params) => {
  return request({
    url: '/user/list',
    method: 'get',
    params
  })
}

/**
 * 添加用户
 */
export const addUser = (data) => {
  return request({
    url: '/user/add',
    method: 'post',
    data
  })
}

/**
 * 更新用户
 */
export const updateUser = (data) => {
  return request({
    url: '/user/update',
    method: 'put',
    data
  })
}

/**
 * 删除用户
 */
export const deleteUser = (userId) => {
  return request({
    url: `/user/delete/${userId}`,
    method: 'delete'
  })
}

/**
 * 上传用户头像
 */
export const uploadAvatar = (userId, file) => {
  const formData = new FormData()
  formData.append('file', file)
  
  return request({
    url: `/user/avatar/upload/${userId}`,
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 更新用户头像URL
 */
export const updateAvatar = (userId, avatarUrl) => {
  return request({
    url: `/user/avatar/${userId}`,
    method: 'put',
    params: { avatarUrl }
  })
}

/**
 * 健康检查
 */
export const healthCheck = () => {
  return request({
    url: '/user/health',
    method: 'get'
  })
}

/**
 * 搜索用户
 */
export const searchUsers = (params) => {
  return request({
    url: '/user/search',
    method: 'get',
    params
  })
}

/**
 * 导出用户列表
 */
export const exportUserList = (params) => {
  return request({
    url: '/user/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

/**
 * 下载用户导入模板
 */
export const downloadTemplate = () => {
  return request({
    url: '/user/template',
    method: 'get',
    responseType: 'blob'
  })
}

/**
 * 导入用户数据
 */
export const importUserData = (file) => {
  const formData = new FormData()
  formData.append('file', file)
  
  return request({
    url: '/user/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}